/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.unboundid.ldap.sdk.LDAPConnectionPool;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapServerConfig.ExternalLdapConfig;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.unboundid.LdapConnectionPool;
/*     */ import com.zimbra.cs.ldap.unboundid.UBIDLdapContext;
/*     */ import com.zimbra.qa.unittest.prov.LocalconfigTestUtil;
/*     */ import com.zimbra.qa.unittest.prov.ProvTest.SkipTestReason;
/*     */ import java.io.PrintStream;
/*     */ import java.net.InetAddress;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Ignore;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestLdapConnection
/*     */   extends LdapTest
/*     */ {
/*     */   private static final boolean START_TLS_ENABLED = false;
/*  48 */   private static final String BIND_DN = LC.zimbra_ldap_userdn.value();
/*  49 */   private static final String BIND_PASSWORD = LC.zimbra_ldap_password.value();
/*     */   private static String LDAP_URL_ON_CHECKOUT;
/*     */   private static String LDAP_URL_AFTER_EXCEPTION;
/*     */   private static String LDAP_URL_BACKGROUND;
/*     */   
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/*  58 */     LDAP_URL_ON_CHECKOUT = "ldap://" + InetAddress.getLocalHost().getHostName() + ":389";
/*  59 */     LDAP_URL_AFTER_EXCEPTION = "ldap://" + LC.zimbra_server_hostname.value() + ":389";
/*  60 */     LDAP_URL_BACKGROUND = "ldap://localhost:389";
/*     */   }
/*     */   
/*     */   private UBIDLdapContext getContext() throws Exception {
/*  64 */     return (UBIDLdapContext)LdapClient.getContext(LdapUsage.UNITTEST);
/*     */   }
/*     */   
/*     */   private UBIDLdapContext getContext(LdapServerConfig.ExternalLdapConfig ldapConfig) throws Exception {
/*  68 */     return (UBIDLdapContext)LdapClient.getExternalContext(ldapConfig, LdapUsage.UNITTEST);
/*     */   }
/*     */   
/*     */   private void closeContext(UBIDLdapContext zlc) {
/*  72 */     LdapClient.closeContext(zlc);
/*     */   }
/*     */   
/*     */   private void stopLdap() throws Exception {
/*  76 */     List<String> STOP_LDAP_CMD = new ArrayList();
/*  77 */     STOP_LDAP_CMD.add("/opt/zimbra/bin/ldap");
/*  78 */     STOP_LDAP_CMD.add("stop");
/*     */     
/*     */ 
/*  81 */     ProcessBuilder pb = new ProcessBuilder(STOP_LDAP_CMD);
/*  82 */     Process process = pb.start();
/*  83 */     int exitValue = process.waitFor();
/*  84 */     Assert.assertEquals(0L, exitValue);
/*     */     
/*     */ 
/*  87 */     Thread.sleep(2000L);
/*     */   }
/*     */   
/*     */   private void startLdap() throws Exception {
/*  91 */     List<String> STOP_LDAP_CMD = new ArrayList();
/*  92 */     STOP_LDAP_CMD.add("/opt/zimbra/bin/ldap");
/*  93 */     STOP_LDAP_CMD.add("start");
/*     */     
/*     */ 
/*  96 */     ProcessBuilder pb = new ProcessBuilder(STOP_LDAP_CMD);
/*  97 */     Process process = pb.start();
/*  98 */     int exitValue = process.waitFor();
/*  99 */     Assert.assertEquals(0L, exitValue);
/*     */     
/*     */ 
/* 102 */     Thread.sleep(2000L);
/*     */   }
/*     */   
/*     */   private LDAPConnectionPool populateConnPool(LdapServerConfig.ExternalLdapConfig ldapConfig, int numConns) throws Exception
/*     */   {
/* 107 */     int MAX_POOL_SIZE = LC.ldap_connect_pool_maxsize.intValue();
/* 108 */     Assert.assertTrue(numConns < MAX_POOL_SIZE);
/*     */     
/* 110 */     LDAPConnectionPool connPool = null;
/*     */     
/* 112 */     List<UBIDLdapContext> zlcs = Lists.newArrayList();
/* 113 */     for (int i = 0; i < numConns; i++) {
/* 114 */       UBIDLdapContext zlc = getContext(ldapConfig);
/* 115 */       zlcs.add(zlc);
/*     */       
/* 117 */       if (connPool == null) {
/* 118 */         connPool = zlc.getConnectionPool();
/*     */       }
/*     */       else
/*     */       {
/* 122 */         Assert.assertTrue(connPool == zlc.getConnectionPool());
/*     */       }
/*     */     }
/*     */     
/* 126 */     Assert.assertEquals(MAX_POOL_SIZE, connPool.getMaximumAvailableConnections());
/*     */     
/*     */ 
/* 129 */     Assert.assertEquals(0L, connPool.getCurrentAvailableConnections());
/*     */     
/* 131 */     for (int i = 0; i < numConns; i++) {
/* 132 */       UBIDLdapContext zlc = (UBIDLdapContext)zlcs.get(i);
/* 133 */       closeContext(zlc);
/*     */     }
/*     */     
/*     */ 
/* 137 */     Assert.assertEquals(numConns, connPool.getCurrentAvailableConnections());
/*     */     
/* 139 */     return connPool;
/*     */   }
/*     */   
/*     */   private Map<KnownKey, String> setLocalConfig(Map<KnownKey, String> keyValues) {
/* 143 */     Map<KnownKey, String> curValues = new HashMap();
/*     */     
/* 145 */     for (Map.Entry<KnownKey, String> keyValue : keyValues.entrySet()) {
/* 146 */       KnownKey key = (KnownKey)keyValue.getKey();
/* 147 */       String value = (String)keyValue.getValue();
/*     */       
/*     */ 
/* 150 */       String curValue = key.value();
/* 151 */       curValues.put(key, curValue);
/*     */       
/*     */ 
/* 154 */       LocalconfigTestUtil.modifyLocalConfigTransient(key, value);
/* 155 */       Assert.assertEquals(value, key.value());
/*     */     }
/*     */     
/* 158 */     return curValues;
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void onCheckoutHealthCheck() throws Exception {
/* 163 */     SKIP_FOR_INMEM_LDAP_SERVER(ProvTest.SkipTestReason.CONNECTION_POOL_HEALTH_CHECK);
/*     */     
/* 165 */     Map<KnownKey, String> lcKeysToModify = new HashMap();
/* 166 */     lcKeysToModify.put(LC.ldap_connect_pool_health_check_on_checkout_enabled, "true");
/*     */     
/*     */ 
/*     */ 
/* 170 */     Map<KnownKey, String> origLCKeyValues = setLocalConfig(lcKeysToModify);
/*     */     
/* 172 */     int NUM_CONNS = 10;
/*     */     
/* 174 */     LdapServerConfig.ExternalLdapConfig ldapConfig = new LdapServerConfig.ExternalLdapConfig(LDAP_URL_ON_CHECKOUT, false, null, BIND_DN, BIND_PASSWORD, null, null);
/*     */     
/*     */ 
/* 177 */     LDAPConnectionPool connPool = populateConnPool(ldapConfig, 10);
/*     */     
/*     */ 
/* 180 */     System.out.println("Before health check, availConns = " + connPool.getCurrentAvailableConnections());
/* 181 */     stopLdap();
/*     */     
/*     */ 
/* 184 */     boolean caughtException = false;
/*     */     try {
/* 186 */       zlc = getContext(ldapConfig);
/*     */     } catch (ServiceException e) { UBIDLdapContext zlc;
/* 188 */       caughtException = true;
/*     */     }
/* 190 */     Assert.assertTrue(caughtException);
/*     */     
/* 192 */     System.out.println("After health check, availConns = " + connPool.getCurrentAvailableConnections());
/* 193 */     Assert.assertEquals(0L, connPool.getCurrentAvailableConnections());
/*     */     
/*     */ 
/* 196 */     setLocalConfig(origLCKeyValues);
/*     */     
/* 198 */     startLdap();
/*     */     
/*     */ 
/* 201 */     UBIDLdapContext zlc = getContext(ldapConfig);
/* 202 */     closeContext(zlc);
/*     */   }
/*     */   
/*     */   @Test
/*     */   @Ignore
/*     */   public void afterExceptionHealthCheck() throws Exception {
/* 208 */     Map<KnownKey, String> lcKeysToModify = new HashMap();
/* 209 */     lcKeysToModify.put(LC.ldap_connect_pool_health_check_on_checkout_enabled, "false");
/*     */     
/*     */ 
/*     */ 
/* 213 */     Map<KnownKey, String> origLCKeyValues = setLocalConfig(lcKeysToModify);
/*     */     
/* 215 */     int NUM_CONNS = 10;
/*     */     
/* 217 */     LdapServerConfig.ExternalLdapConfig ldapConfig = new LdapServerConfig.ExternalLdapConfig(LDAP_URL_AFTER_EXCEPTION, false, null, BIND_DN, BIND_PASSWORD, null, null);
/*     */     
/*     */ 
/* 220 */     LDAPConnectionPool connPool = populateConnPool(ldapConfig, 10);
/*     */     
/*     */ 
/* 223 */     System.out.println("Before health check, availConns = " + connPool.getCurrentAvailableConnections());
/* 224 */     stopLdap();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 229 */     UBIDLdapContext zlcTest = getContext(ldapConfig);
/*     */     
/*     */ 
/*     */ 
/* 233 */     boolean caughtException = false;
/*     */     try {
/* 235 */       zlcTest.getAttributes("", null);
/* 236 */     } catch (ServiceException e) { e = 
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 246 */         e;caughtException = true; } finally {}
/* 247 */     Assert.assertTrue(caughtException);
/*     */     
/* 249 */     System.out.println("After health check, availConns = " + connPool.getCurrentAvailableConnections());
/*     */     
/* 251 */     int secs = 0;
/*     */     for (;;) {
/* 253 */       Thread.sleep(1000L);
/* 254 */       secs++;
/* 255 */       int junk = connPool.getCurrentAvailableConnections();
/* 256 */       System.out.println("After health check, availConns = " + junk + " " + secs);
/* 257 */       if (junk < 10) {
/*     */         break;
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 266 */     Assert.assertEquals(9L, connPool.getCurrentAvailableConnections());
/*     */     
/*     */ 
/* 269 */     setLocalConfig(origLCKeyValues);
/*     */     
/* 271 */     startLdap();
/*     */     
/*     */ 
/* 274 */     UBIDLdapContext zlc = getContext(ldapConfig);
/* 275 */     closeContext(zlc);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void backgroundHealthCheck() throws Exception
/*     */   {
/* 281 */     SKIP_FOR_INMEM_LDAP_SERVER(ProvTest.SkipTestReason.CONNECTION_POOL_HEALTH_CHECK);
/*     */     
/* 283 */     long BACKGROUND_HEALTH_CHECK_INTERVAL = 5000L;
/*     */     
/* 285 */     Map<KnownKey, String> lcKeysToModify = new HashMap();
/* 286 */     lcKeysToModify.put(LC.ldap_connect_pool_health_check_on_checkout_enabled, "false");
/*     */     
/*     */ 
/* 289 */     lcKeysToModify.put(LC.ldap_connect_pool_health_check_background_interval_millis, Long.valueOf(5000L).toString());
/*     */     
/*     */ 
/* 292 */     Map<KnownKey, String> origLCKeyValues = setLocalConfig(lcKeysToModify);
/*     */     
/* 294 */     int MAX_POOL_SIZE = LC.ldap_connect_pool_maxsize.intValue();
/* 295 */     int NUM_CONNS = 10;
/*     */     
/* 297 */     LdapServerConfig.ExternalLdapConfig ldapConfig = new LdapServerConfig.ExternalLdapConfig(LDAP_URL_BACKGROUND, false, null, BIND_DN, BIND_PASSWORD, null, null);
/*     */     
/*     */ 
/* 300 */     LDAPConnectionPool connPool = populateConnPool(ldapConfig, 10);
/*     */     
/*     */ 
/* 303 */     System.out.println("Before health check, availConns = " + connPool.getCurrentAvailableConnections());
/* 304 */     stopLdap();
/*     */     
/*     */ 
/* 307 */     long waitFor = 6000L;
/* 308 */     System.out.println("Waiting for " + waitFor + " msecs");
/* 309 */     Thread.sleep(waitFor);
/*     */     
/* 311 */     System.out.println("After health check, availConns = " + connPool.getCurrentAvailableConnections());
/* 312 */     Assert.assertEquals(0L, connPool.getCurrentAvailableConnections());
/*     */     
/*     */ 
/* 315 */     setLocalConfig(origLCKeyValues);
/*     */     
/* 317 */     startLdap();
/*     */     
/*     */ 
/* 320 */     UBIDLdapContext zlc = getContext(ldapConfig);
/* 321 */     closeContext(zlc);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testConnPoolNumAvailConns()
/*     */     throws Exception
/*     */   {
/* 328 */     int INIT_POOL_SIZE = LC.ldap_connect_pool_initsize.intValue();
/* 329 */     int MAX_POOL_SIZE = LC.ldap_connect_pool_maxsize.intValue();
/*     */     
/* 331 */     LDAPConnectionPool connPool = LdapConnectionPool.getConnPoolByName("ZimbraReplica");
/*     */     
/*     */ 
/* 334 */     Assert.assertEquals(INIT_POOL_SIZE, connPool.getCurrentAvailableConnections());
/* 335 */     Assert.assertEquals(MAX_POOL_SIZE, connPool.getMaximumAvailableConnections());
/*     */     
/* 337 */     UBIDLdapContext zlc = getContext();
/* 338 */     String poolName = connPool.getConnectionPoolName();
/* 339 */     closeContext(zlc);
/*     */     
/* 341 */     Assert.assertEquals("ZimbraReplica", poolName);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 350 */     for (int i = 0; i < 10; i++) {
/* 351 */       UBIDLdapContext conn = getContext();
/* 352 */       closeContext(conn);
/* 353 */       Assert.assertEquals(INIT_POOL_SIZE, connPool.getCurrentAvailableConnections());
/*     */     }
/*     */     
/* 356 */     int numOpen = 20;
/*     */     
/* 358 */     Assert.assertTrue(numOpen > INIT_POOL_SIZE);
/* 359 */     Assert.assertTrue(numOpen < MAX_POOL_SIZE);
/*     */     
/*     */ 
/*     */ 
/* 363 */     UBIDLdapContext[] conns = new UBIDLdapContext[numOpen];
/* 364 */     for (int i = 0; i < numOpen; i++) {
/* 365 */       conns[i] = getContext();
/* 366 */       int expected = Math.max(0, INIT_POOL_SIZE - (i + 1));
/* 367 */       Assert.assertEquals(expected, connPool.getCurrentAvailableConnections());
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 372 */     for (int i = 0; i < numOpen; i++) {
/* 373 */       closeContext(conns[i]);
/* 374 */       int expected = i + 1;
/* 375 */       Assert.assertEquals(expected, connPool.getCurrentAvailableConnections());
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestLdapConnection.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */